<?php
namespace app\common\model;

use think\Model;
use think\Db;
use think\Config;
use think\Loader;
class Qbank extends Model
{
    // 指定表名,不含前缀
    protected $name = 'qbank';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
	public $error;
	
	public function getError()
	{
		return $this->error;		
	}
	
	public function tablename()
	{
		return 'ex_'.$this->name;		
	}
	
	public function setError($str){
		$this->error = $str;
	}
	
	public function getQbankList($parentid=-1)
	{
		if($parentid>=0) $sql_where['parentid'] = $parentid;
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
		
		$admin_plaform_id = Db::name('admin_user')->where(['id'=>$_SESSION["think"]["auth_id"]])->field('plaform_id')->find();
		$association = Db::name('qbank_association')->where(['plaformid'=>$admin_plaform_id["plaform_id"],'marks'=>0,'status'=>0])->field('qb_id')->select();
		$arr = array();
		foreach($association as $v){
			$arr[] = $v['qb_id'];
		}

		$result = Db::name($this->name)->where(['admin_reg'=>['in',$child_plaform]])->where('isdelete=0')->where($sql_where);
		if(!empty($arr)){
			$result = $result->whereOr(['id'=>['in',$arr]]);
		}
		$result = $result->select();
	
		return $result;
	}

	public function getInfo($id){
		$result = Db::name($this->name)->where('isdelete=0')->where('id',$id)->find();
		return $result;
	}


	public function getListByArg(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->select();
        return $arg;
    }
	public function getListByArgOne(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->find();
        return $arg;
    }
	
	/**
     * 获得章节
     */
    public function sections($subjectid =0){
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);

        $sqlwhere = ['admin_reg'=>['in',$child_plaform],'status'=>1,'isdelete'=>0];
		/* 增加当有科目ID时，获取对应科目下所有章节 */
		if(!empty($subjectid)) $sqlwhere['se_subjectid'] = $subjectid;
	
        $arr=Db::name('sections')->field('id,se_name')->where($sqlwhere)->select();
        return $arr;
    }
	
	
	/* 更新或者新增函数 */
	public function DataSave($data){
		$id = $data['id'];
		if(empty($id)) $data['admin_reg'] = $_SESSION['think']['reg'];
		!empty($id) ?  $ret = $this->allowField(true)->isUpdate(true)->save($data, ['id' => $id]) : $ret = $this->allowField(true)->isUpdate(false)->save($data);
		
		return $ret;
	}
	
	/**获取指定题库的 章节知识点 科目**/
	public function getListByArgDetails($sharing_qbank_id){
        $qbanks  = self::getListByArgOne(['id'=>$sharing_qbank_id],'parentid');
		if ($qbanks->parentid){
            $table = 'question_db';
        }else{
            $table = 'questions';
        }
		$q_subjectid = Db::name($table)->field('distinct q_subjectid')->where(['q_qbankid'=>$sharing_qbank_id])->select(); //科目
		$q_sectionid = Db::name($table)->field('distinct q_sectionid')->where(['q_qbankid'=>$sharing_qbank_id])->select(); //章节
		$q_knowsid = Db::name($table)->field('distinct q_knowsid')->where(['q_qbankid'=>$sharing_qbank_id])->select(); //知识
		$datas = ['subjectid'=>[],'sectionid'=>[],'knowsid'=>[]];
		foreach($q_subjectid as $vsub){
			$datas['subjectid'][] = $vsub['q_subjectid'];
		}
		
		foreach($q_sectionid as $vsect){
			$datas['sectionid'][] = $vsect['q_sectionid'];
		}
		
		foreach($q_knowsid as $vknow){
			$datas['knowsid'][] = $vknow['q_knowsid'];
		}
		return $datas;
    }
	

	
}
